<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">型号名称：</label>
                  <div class="layui-input-inline">
                    <input
                      name="name"
                      type="text"
                      class="layui-input"
                      placeholder="请输入型号名称"
                      autocomplete="off"
                    />
                  </div>
                </div>
                <div class="layui-inline">
                  <button
                    class="layui-btn icon-btn color-green"
                    lay-filter="formSubSearchLog"
                    lay-submit
                    layui-form-keyDownSearch="true"
                  >
                    查询
                  </button>
                  <button
                    class="layui-btn icon-btn color-white"
                    lay-filter="resets"
                    lay-submit
                  >
                    重置
                  </button>
                </div>
              </div>
              <div class="query-rt"></div>
            </div>
          </div>
        </div>
        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <a id="deviceModelBtnAdd" class="layui-btn color-reseda">添加</a>
              <button class="layui-btn icon-btn color-reseda">
                <input
                  id="uploaderInput"
                  type="file"
                  name="file"
                  class="import"
                  accept=".xls,.xlsx"
                />导入
              </button>
              <button
                id="configFun"
                style="display: none"
                class="layui-btn icon-btn color-reseda"
                type="button"
              >
                确认选择
              </button>
            </div>
            <div class="lay-btn-rt">
              <button
                id="deviceModelBtnTemplate"
                class="layui-btn icon-btn color-green"
              >
                导出
              </button>
              <a id="deviceModelDle" class="layui-btn layui-btn-danger"
                >批量删除</a
              >
            </div>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <table
        class="layui-table"
        id="deviceModelTable"
        lay-filter="deviceModelTable"
      ></table>
    </div>
  </div>
</div>

<style>
  .red {
    color: red;
  }
  .import {
    opacity: 0;
    position: absolute;
    width: 69px;
    height: 38px;
    margin-left: -10px;
  }
  .deviceModelForm label {
    width: 120px;
  }
  .borderNone {
    border: none;
  }
  .companyFormPhotos {
    /* margin-left:140px; */
    line-height: 38px;
    float: left;
  }
  .companyFormPhotos img {
    cursor: pointer;
    height: 38px;
    width: auto;
    max-width: 200px;
  }
  .deviceModelForm .layui-input-block {
    margin-left: 150px;
  }
</style>
<!-- 表格操作列 -->
<script type="text/html" id="deviceModelTableBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="details"
    >详情</a
  >
  <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="deviceModelForm">
  <form
    lay-filter="deviceModelForm"
    class="layui-form model-form deviceModelForm"
    style="padding-bottom: 10px"
  >
    <input name="id" type="hidden" />
    <div class="layui-form-item">
      <div class="layui-col-md6">
        <label class="layui-form-label"
          ><span class="red">*</span>设备类型</label
        >
        <div class="layui-input-block">
          <select
            name="deviceType"
            lay-filter="deviceModel"
            lay-verify="required"
            lay-search
            required
            id="deviceModel"
          ></select>
        </div>

        <!-- 树结构 -->
        <!--                <input name="deviceType" value="" type="hidden">-->
        <!--                <div class="layui-input-inline mr0" id="selectProductType">-->
        <!--                </div>-->
      </div>
      <div class="layui-col-md6">
        <label class="layui-form-label"
          ><span class="red">*</span>设备品牌</label
        >
        <div class="layui-input-block">
          <select
            name="deviceBrand"
            lay-filter="deviceBrand"
            lay-verify="required"
            lay-search
            required
            id="deviceBrand"
          ></select>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-col-md6">
        <label class="layui-form-label"
          ><span class="red">*</span>设备型号名称</label
        >
        <div class="layui-input-block">
          <input
            name="name"
            placeholder="请输入设备型号"
            type="text"
            class="layui-input"
            lay-verify="required"
            autocomplete="off"
          />
        </div>
      </div>
      <div class="layui-col-md6">
        <label class="layui-form-label"
          ><span class="red">*</span>设备型号编码</label
        >
        <div class="layui-input-block">
          <input
            name="modelNo"
            placeholder="请输入设备型号编码"
            type="text"
            class="layui-input"
            lay-verify="required"
            autocomplete="off"
          />
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-col-md6">
        <label class="layui-form-label"
          ><span class="red">*</span>设备性质</label
        >
        <div class="layui-input-block">
          <select
            name="type"
            lay-filter="type"
            id="deviceNature"
          >
        <option value="1">生产设备</option>
        <option value="2">质检设备</option>
        </select>
        </div>
      </div>
      <div class="layui-col-md6">
        <label class="layui-form-label"
          ><span class="red">*</span>设备编码头</label
        >
        <div class="layui-input-block">
          <input
            name="deviceHead"
            placeholder="请输入设备编码头"
            type="text"
            class="layui-input"
            lay-verify="required"
            autocomplete="off"
          />
        </div>
      </div>
     
    </div>
    <div class="layui-form-item">
      <div class="layui-col-md6">
        <label class="layui-form-label">厂家名称</label>
        <div class="layui-input-block">
          <input
            name="factoryName"
            id="factoryName"
            type="text"
            class="layui-input"
            required
            autocomplete="off"
          />
        </div>
        <!--                <button type="button" class="layui-btn" id="factoryIdAdd">点击添加</button>-->
      </div>
      <div class="layui-col-md6">
        <label class="layui-form-label">厂家联系人</label>
        <div class="layui-input-block">
          <input
            name="factoryContact"
            id="factoryContact"
            type="text"
            class="layui-input"
            autocomplete="off"
          />
        </div>
      </div>
    </div>
    <!--        <div class="layui-form-item">-->
    <!--            <label class="layui-form-label"><span class="red">*</span>供应商</label>-->
    <!--            <div class="layui-input-inline">-->
    <!--                <select name="supplier" lay-filter="supplier" lay-verify="required" required id="supplier"></select>-->
    <!--            </div>-->
    <!--        </div>-->
    <!--        <div class="layui-form-item">-->
    <!--            <label class="layui-form-label">供应商联系人</label>-->
    <!--            <div class="layui-input-inline">-->
    <!--                <input name="supplierLink"  type="text" class="layui-input" lay-verify="required" />-->
    <!--            </div>-->
    <!--        </div>-->
    <!--        <div class="layui-form-item">-->

    <!--            <button type="button" class="layui-btn" id="factoryIdAdd">点击添加</button>-->
    <!--        </div>-->
    <!-- <label class="layui-form-label">厂家联系人</label>-->
    <!--        <input name="factoryName" id="factoryName"  type="hidden" class="layui-input"/>-->

    <div class="layui-form-item">
      <div class="layui-col-md6">
        <label class="layui-form-label">厂家网站</label>
        <div class="layui-input-block">
          <input
            name="factoryUrl"
            id="factoryUrl"
            type="text"
            class="layui-input"
            autocomplete="off"
          />
        </div>
      </div>
      <div class="layui-col-md6">
        <label class="layui-form-label">备注说明</label>
        <div class="layui-input-block">
          <input
            name="remark"
            type="text"
            class="layui-input"
            autocomplete="off"
          />
        </div>
      </div>
    </div>
    <div class="layui-form-item" style="padding-bottom: 20px">
      <div class="layui-col-md6">
        <label class="layui-form-label">设备照片</label>
        <div class="layui-input-block">
          <div id="btnUploadCredentials_path" class="companyFormPhotos"></div>
          <div id="btnUploadCredentials" class="layui-btn" style="float: left;">
            <i class="layui-icon">&#xe681;</i>上传图片
          </div>
          <input
            name="devicePic"
            type="hidden"
            class="layui-input"
            lay-verify=""
            id="devicePic"
          />
        </div>
      </div>
      <div class="layui-col-md6">
        <label class="layui-form-label">设备资料</label>
        <div class="layui-input-block">
          <div id="btnUploadDviceAppendix_path" class="companyFormPhotos"></div>
          <div
            id="btnUploadDviceAppendix"
            class="layui-btn"
            style="float: left;"
          >
            <i class="layui-icon">&#xe681;</i>上传
          </div>
          <input
            name="deviceAppendix"
            type="hidden"
            class="layui-input"
            lay-verify=""
            id="deviceAppendix"
          />
        </div>
      </div>
    </div>
    <div class="layui-form-item" style="padding-bottom: 20px">
      <div class="layui-col-md6">
        <label class="layui-form-label">设备SOP资料</label>
        <div class="layui-input-block">
          <div id="btnUploadReferenceUrl_path" class="companyFormPhotos"></div>
          <div
            id="btnUploadReferenceUrl"
            class="layui-btn"
            style="float: left;"
          >
            <i class="layui-icon">&#xe681;</i>上传
          </div>
          <input
            name="referenceUrl"
            type="hidden"
            class="layui-input"
            lay-verify=""
            id="referenceUrl"
          />
        </div>
      </div>
    </div>
    <div class="layui-form-item" style="padding-bottom: 20px">
      <div class="layui-col-md6">
        <label class="layui-form-label">设备操作说明</label>
        <div class="layui-input-block">
          <div id="btnUploadDirectionUrl_path" class="companyFormPhotos"></div>
          <div
            id="btnUploadDirectionUrl"
            class="layui-btn"
            style="float: left;"
          >
            <i class="layui-icon">&#xe681;</i>上传
          </div>
          <input
            name="directionUrl"
            type="hidden"
            class="layui-input"
            lay-verify=""
            id="directionUrl"
          />
        </div>
      </div>
    </div>
    <div class="layui-form-item" style="padding-bottom: 20px">
      <div class="layui-col-md6">
        <label class="layui-form-label">其他附件资料</label>
        <div class="layui-input-block">
          <div id="btnUploadOtherUrl_path" class="companyFormPhotos"></div>
          <div id="btnUploadOtherUrl" class="layui-btn" style="float: left;">
            <i class="layui-icon">&#xe681;</i>上传
          </div>
          <input
            name="otherUrl"
            type="hidden"
            class="layui-input"
            lay-verify=""
            id="otherUrl"
          />
        </div>
      </div>
    </div>
    <div class="layui-form-item text-right pd-20">
      <button
        class="layui-btn layui-btn-primary"
        type="button"
        ew-event="closeDialog"
      >
        取消
      </button>
      <button
        class="layui-btn"
        lay-filter="deviceModelFormSubmit"
        lay-submit
        id="deviceModelFormSubmit"
      >
        提交
      </button>
    </div>
  </form>
</script>

<script type="text/html" id="searchManufactureForm">
  <form
    lay-filter="searchManufactureForm"
    class="layui-form model-form"
    style="padding:20px"
  >
    <div class="layui-input-inline">
      <div class="layui-input-inline">
        <input type="text" class="layui-input" id="searchManufactureIpt" />
      </div>
      <button type="button" class="layui-btn" id="searchManufactureBtn">
        <i class="layui-icon">&#xe615;</i>
      </button>
    </div>
  </form>
</script>

<!-- js部分 -->
<script>
  layui.use(["layer", "form", "table", "admin", "upload"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;

    var parentId;
    let nowData = {};
    let where = {};
    if (typeof parentData !== "undefined") {
      nowData = { ...parentData };
      if (nowData.eventName == "radio" || nowData.eventName == "checkbox") {
        $("#configFun").show();
      }
      if (nowData.where) {
        where = nowData.where;
      }
    }
    admin.reqSync(
      "enterprise/dict/admin/dictLabel/list?page=1&limit=10&dictType=sys_device_type_code",
      {},
      function (res) {
        if (res.code == 200) {
          parentId = res.data[0].labelValue;
        }
      }
    );
    //厂家信息 "name":"湖南云端商业管理有限公司"
    let facFactoryData = [];
    admin.req(
      "enterprise/factory/admin/facFactory/list",
      {},
      function (res) {
        if (res.code == 200) {
          facFactoryData = res.data;
        }
      },
      "GET"
    );

    // 渲染表格
    var insTb = table.render({
      elem: "#deviceModelTable",
      url: config.base + "enterprise/device/admin/deviceModel/list",
      toolbar: true,
      defaultToolbar: ["filter"],
      where: {...where},
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          {
            type: "checkbox",
            hide: nowData.eventName == "radio" ? true : false,
          },
          { type: "radio", hide: nowData.eventName == "radio" ? false : true },
          // ,{field: 'id', sort: true, title: 'ID'}
          // ,{type: 'numbers', title: '序号'}
          { field: "name", sort: true, title: "设备型号名称" },
          { field: "modelNo", sort: true, title: "设备型号编码" },
          { field: "deviceBrandName", sort: true, title: "所属品牌" },
          { field: "deviceTypeName", sort: true, title: "设备类型" },
          { field: "factoryName", sort: true, title: "所属厂家" },
          { field: "remark", sort: true, title: "备注描述" },
          { field: "fixtureAmount", sort: true, title: "数量" },
          { field: "unit", sort: true, title: "单位" },
          { field: "createTime", sort: true, title: "添加时间" },
          {
            align: "center",
            toolbar: "#deviceModelTableBar",
            title: "操作",
            width: 200,
          },
        ],
      ],
    });

    var flag = true;
    // 工具条点击事件
    table.on("tool(deviceModelTable)", function (obj) {
      var data = obj.data;
      if (obj.event === "details") {
        //详情
        flag = true;
        showEditModel(data);
      } else if (obj.event === "del") {
        // 删除
        doDelete(obj);
      } else if (obj.event === "edit") {
        //修改
        flag = false;
        showEditModel(data);
      }
    });

    //监听排序
    table.on("sort(deviceModelTable)", function (obj) {
      table.reload("deviceModelTable", {
        initSort: obj,
        where: {
          sort: obj.field,
          order: obj.type,
        },
      });
    });

    // 搜索
    form.on("submit(formSubSearchLog)", function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
    });

    // 重置搜索
    form.on("submit(resets)", function (data) {
      $(".lay-header").find("select").val("");
      $(".lay-header").find("input").val("");
      let field = data.field;
      for (let key in field) {
        field[key] = "";
      }
      insTb.reload({ where: field, page: { curr: 1 } }, "data");
    });

    // 添加按钮点击事件
    $("#deviceModelBtnAdd").click(function () {
      showEditModel();
    });
    $("#configFun").click(function () {
      let list = table.checkStatus("deviceModelTable").data;
      childData.list = list;
      layer.close(nowData.zindex);
    });
    // 显示编辑弹窗
    function showEditModel(data) {
      admin.open({
        type: 1,
        area: "900px",
        offset: "65px",
        title: data ? (flag ? "设备型号详情" : "修改设备型号") : "添加设备型号",
        content: $("#deviceModelForm").html(),
        success: function () {
          // if(facFactoryData.length>0){
          //     loadSelectHtml(facFactoryData, "请选择厂家", "factoryId",1)
          // }
          if (data && flag) {
            // 后续可能要用
            // facFactoryData.forEach(item=>{
            //     if(data.factoryId == item.id){
            //         // data.factoryName = item.factoryName;
            //         data.factoryContact = item.factoryContact;
            //         data.factoryAddress = item.factoryAddress;
            //         data.factoryPhone = item.factoryPhone;
            //         data.factoryUrl = item.factoryUrl;
            //         // data.factoryName = item.factoryName;
            //     }
            // })

            form.val("deviceModelForm", data);
            $("#btnUploadCredentials_path").html(
              "<img src='" + data.devicePic + "'/>"
            );
            $("#btnUploadDviceAppendix_path").html(
              "<img src='" + data.deviceAppendix + "'/>"
            );
            $("#btnUploadReferenceUrl_path").html(
              "<img src='" + data.referenceUrl + "'/>"
            );
            $("#btnUploadDirectionUrl_path").html(
              "<img src='" + data.directionUrl + "'/>"
            );
            $("#btnUploadOtherUrl_path").html(
              "<img src='" + data.otherUrl + "'/>"
            );

            $.each($(".deviceModelForm input"), (index, value) => {
              $(value).attr("disabled", "");
            });
            $.each($(".deviceModelForm select"), (index, value) => {
              $(value).attr("disabled", "");
            });

            $("#deviceModel").html(
              '<option value="">' + data.deviceTypeName + "</option>"
            );
            $("#deviceBrand").html(
              '<option value="">' + data.deviceBrand + "</option>"
            );
            $("#supplier").html(
              '<option value="">' + data.supplier + "</option>"
            );
            $("#deviceModelFormSubmit").css("display", "none");
            $("#btnUploadCredentials").css("display", "none");
            $("#btnUploadDviceAppendix").css("display", "none");
            $("#factoryIdAdd").hide();
          } else if (data && !flag) {
            // 后续可能要用
            // facFactoryData.forEach(item=>{
            //     if(data.factoryId == item.id){
            //         data.factoryContact = item.factoryContact;
            //         data.factoryAddress = item.factoryAddress;
            //         data.factoryPhone = item.factoryPhone;
            //         data.factoryUrl = item.factoryUrl;
            //     }
            // })

            $("#btnUploadCredentials_path").html(
              "<img src='" + data.devicePic + "'/>"
            );
            $("#btnUploadDviceAppendix_path").html(
              "<img src='" + data.deviceAppendix + "'/>"
            );
            $("#btnUploadReferenceUrl_path").html(
              "<img src='" + data.referenceUrl + "'/>"
            );
            $("#btnUploadDirectionUrl_path").html(
              "<img src='" + data.directionUrl + "'/>"
            );
            $("#btnUploadOtherUrl_path").html(
              "<img src='" + data.otherUrl + "'/>"
            );
            //设备类型

            admin.reqSync(
              "enterprise/product/admin/productType/list?page=1&limit=9999&parentId=" +
                parentId,
              {},
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  loadSelectHtml(res.data, "请选择设备类型", "deviceType");
                }
              },
              "GET"
            );

            //树类型
            //加载设备类型
            // admin.initDataSelectTree(true, 'enterprise/device/admin/deviceType/getAllTypeOnlyPro', null, "selectProductType", data ? data.deviceType : null, "请选择设备类型", "deviceType", null);

            $("#factoryIdAdd").click(function () {
              showSearchManufactureForm();
            });
            //设备品牌
            admin.reqSync(
              "enterprise/device/admin/deviceBrand/list?page=1&limit=9999",
              {},
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  loadSelectHtml(res.data, "请选择设备品牌", "deviceBrand");
                }
              },
              "GET"
            );

            //供应商supplier
            // admin.req(
            //   // "enterprisecab/cabinet/app/supplier/list?page=1&limit=9999",
            //   "enterprise/purchase/admin/supplierInfoPass/list",

            //   { limit: 9999 },
            //   function (res) {
            //     layer.closeAll("loading");
            //     if (res.code == 200) {
            //       loadSelectHtml(res.data, "请选择供应商", "supplier", 5);
            //     }
            //   },
            //   "GET"
            // );
            form.val("deviceModelForm", data);
            $("#btnUploadCredentials").click(
              uploadImg(
                "btnUploadCredentials",
                "btnUploadCredentials_path",
                "devicePic"
              )
            );
            $("#btnUploadDviceAppendix").click(
              uploadImg(
                "btnUploadDviceAppendix",
                "btnUploadDviceAppendix_path",
                "deviceAppendix"
              )
            );
            $("#btnUploadReferenceUrl_path").click(
              uploadFile(
                "btnUploadReferenceUrl",
                "btnUploadReferenceUrl_path",
                "referenceUrl"
              )
            );
            $("#btnUploadDirectionUrl_path").click(
              uploadFile(
                "btnUploadDirectionUrl",
                "btnUploadDirectionUrl_path",
                "directionUrl"
              )
            );
            $("#btnUploadOtherUrl_path").click(
              uploadFile(
                "btnUploadOtherUrl",
                "btnUploadOtherUrl_path",
                "otherUrl"
              )
            );
          } else {
            //设备类型
            admin.reqSync(
              "enterprise/product/admin/productType/list?page=1&limit=9999&parentId=" +
                parentId,
              {},
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  loadSelectHtml(res.data, "请选择设备类型", "deviceType");
                }
              },
              "GET"
            );

            // admin.initDataSelectTree(true, 'enterprise/device/admin/deviceType/getAllTypeOnlyPro', null, "selectProductType", data ? data.name : null, "请选择设备类型", "productTypeId", null);

            // form.on("select(factoryId)", function (data) {
            //     facFactoryData.forEach((item,index)=>{
            //         if(data.value == item.id){
            //             $("#factoryUrl").val(item.factoryUrl)
            //             $("#factoryContact").val(item.factoryContact);
            //             $("#factoryName").val(item.factoryName)
            //         }
            //     })
            // })
            $("#factoryIdAdd").click(function () {
              showSearchManufactureForm();
            });

            //设备品牌
            admin.req(
              "enterprise/device/admin/deviceBrand/list?page=1&limit=9999",
              {},
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  loadSelectHtml(res.data, "请选择设备品牌", "deviceBrand");
                }
              },
              "GET"
            );

            //供应商supplier
            // admin.req(
            //   // "enterprisecab/cabinet/app/supplier/list?page=1&limit=9999",
            //   "enterprise/purchase/admin/supplierInfoPass/list",

            //   { limit: 9999 },

            //   function (res) {
            //     layer.closeAll("loading");
            //     if (res.code == 200) {
            //       loadSelectHtml(res.data, "请选择供应商", "supplier", 5);
            //     }
            //   },
            //   "GET"
            // );
            $("#btnUploadCredentials").click(
              uploadImg(
                "btnUploadCredentials",
                "btnUploadCredentials_path",
                "devicePic"
              )
            );
            $("#btnUploadDviceAppendix").click(
              uploadImg(
                "btnUploadDviceAppendix",
                "btnUploadDviceAppendix_path",
                "deviceAppendix"
              )
            );
            $("#btnUploadReferenceUrl_path").click(
              uploadFile(
                "btnUploadReferenceUrl",
                "btnUploadReferenceUrl_path",
                "referenceUrl"
              )
            );
            $("#btnUploadDirectionUrl_path").click(
              uploadFile(
                "btnUploadDirectionUrl",
                "btnUploadDirectionUrl_path",
                "directionUrl"
              )
            );
            $("#btnUploadOtherUrl_path").click(
              uploadFile(
                "btnUploadOtherUrl",
                "btnUploadOtherUrl_path",
                "otherUrl"
              )
            );
          }
          // 渲染下拉框
          form.render("select");

          // 查看大图
          layer.photos({
            photos: ".companyFormPhotos",
            anim: 5,
          });
          // 表单提交事件
          form.on("submit(deviceModelFormSubmit)", function (d) {
            layer.load(2);
            admin.req(
              data
                ? "enterprise/device/admin/deviceModel/update"
                : "enterprise/device/admin/deviceModel/add",
              d.field,
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  layer.msg(res.msg, { icon: 1 });
                  table.reload("deviceModelTable");
                  layer.closeAll("page");
                } else {
                  layer.msg(res.msg, { icon: 2 });
                }
              },
              data ? "PUT" : "POST"
            );
            return false;
          });
        },
      });
    }

    //搜索添加厂家
    function showSearchManufactureForm() {
      admin.open({
        type: 1,
        area: ["400px", "500px"],
        offset: "100px",
        title: "添加",
        content: $("#searchManufactureForm").html(),
        success: function () {
          $("#searchManufactureBtn").click(() => {
            if ($("#searchManufactureIpt").val()) {
              admin.req(
                "enterprise/factory/admin/facFactory/list",
                { name: $("#searchManufactureIpt").val() },
                function (res) {
                  console.log(res);
                },
                "GET"
              );
            } else {
              layer.msg("请输入厂家名称", { icon: 2 });
            }
          });
        },
      });
    }

    // 上传图片
    function uploadImg(_elem, pathId, inputId) {
      upload.render({
        elem: "#" + _elem,
        url: config.upload_url + "upload",
        data: {},
        accept: "file",
        exts: "jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx",
        headers: { Authorization: "Bearer " + config.getToken() },
        done: function (res) {
          if (res.code == 200) {
            layer.msg(res.msg || "上传成功", { icon: 1 });
            let imagUrl = res.data.url;
            $("#" + pathId).html('<img src="' + imagUrl + '"/>');
            $("#" + inputId).val(imagUrl);
          } else if (res.code == 502) {
            return location.replace("admin/login.html");
          } else {
            layer.msg(res.msg || "上传失败", { icon: 2 });
          }
        },
      });
    }

    //上传附件
    function uploadFile(_elem, pathId, inputId) {
      upload.render({
        elem: "#" + _elem,
        url: config.upload_url + "uploadFile",
        data: {},
        accept: "file",
        exts: "jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx|apk",
        headers: { Authorization: "Bearer " + config.getToken() },
        done: function (res) {
          if (res.code == 200) {
            layer.msg(res.msg || "上传成功", { icon: 1 });
            let imagUrl = res.data.url;
            $("#" + pathId).html(
              "<a href='" + imagUrl + "' target='_blank'>" + imagUrl + "</a>"
            );
            $("#" + inputId).val(imagUrl);
          } else if (res.code == 502) {
            return location.replace("admin/login.html");
          } else {
            layer.msg(res.msg || "上传失败", { icon: 2 });
          }
        },
      });
    }

    // 删除
    function doDelete(obj) {
      let url = "";
      if (typeof obj == "string") {
        url = "enterprise/device/admin/deviceModel/delBatch?ids=" + obj;
      } else {
        url = "enterprise/device/admin/deviceModel/" + obj.data.id;
      }
      layer.confirm(
        "确定要删除吗？",
        {
          offset: "65px",
          skin: "layui-layer-admin",
        },
        function (i) {
          layer.close(i);
          layer.load(2);
          admin.req(
            url,
            {},
            function (res) {
              layer.closeAll("loading");
              if (res.code == 200) {
                layer.msg(res.msg, { icon: 1 });
                table.reload("deviceModelTable");
                // obj.del();
              } else {
                layer.msg(res.msg, { icon: 2 });
              }
            },
            "DELETE"
          );
        }
      );
    }

    // 导出excel
    $("#deviceModelDle").click(function () {
      var checkRows = table.checkStatus("deviceModelTable");
      if (checkRows.data.length == 0) {
        layer.msg("请选择要批量的数据", { icon: 2 });
      } else {
        let ids = [];
        checkRows.data.forEach((item, index) => {
          ids.push(item.id);
        });
        doDelete(ids.join());
      }
    });
    // 模板下载
    $("#deviceModelBtnTemplate").click(function () {
      window.location.href =
        config.base + "enterprise/device/admin/deviceModel/deviceTemplate";
    });
    // 导入excel
    $("#uploaderInput").on("change", function (e) {
      layer.load(2);
      var formData = new FormData();
      var files = e.target.files;
      let that = $(this);
      formData.append("file", files[0]);
      formData.append("name", files[0].name); //这个地方可以传递多个参数
      $.ajax({
        url: config.base + "enterprise/device/admin/deviceModel/importExcel",
        data: formData,
        headers: { Authorization: "Bearer " + config.getToken() },
        type: "POST",
        processData: false,
        contentType: false,
        success: function (res) {
          layer.closeAll("loading");
          if (res.code == 200) {
            layer.msg(res.msg, { icon: 1 });
            layer.closeAll("page");
            setTimeout((_) => {
              location.reload();
            }, 1000);
          } else {
            layer.msg(res.msg, { icon: 2 });
          }
          that.val("");
        },
      });
    });

    // option渲染
    function loadSelectHtml(data, tips, htmltarget, type) {
      let html = " <option value>" + tips + "</option>";
      if (!data || data == null) {
        return;
      }
      for (let i = 0; i < data.length; i++) {
        if (type == 1) {
          html += `<option value='${data[i].id}' id='${data[i].factoryName}'>${data[i].factoryName}</option>`;
        } else if (type == 2) {
          html += `<option value='${data[i].skuCode}'>${data[i].skuCode}</option>`;
        } else if (type == 3) {
          html += `<option value='${data[i].id}'>${data[i].warehouseName}</option>`;
        } else if (type == 4) {
          html += `<option value='${data[i].skuCode}'  skuCode='${data[i].skuCode}'>${data[i].skuName}</option>`;
        } else if (type == 5) {
          html += `<option value='${data[i].companyName}'>${data[i].companyName}</option>`;
        } else {
          //html += `<option value='${data[i].name}'>${data[i].name}</option>`;
          html += `<option value='${data[i].id}' data='${JSON.stringify(
            data[i]
          )}'>${data[i].name}</option>`;
        }
      }

      $("form select[name='" + htmltarget + "']").html(html);
      form.render("select");
    }
  });
</script>
